
""""

python get_dir_size.py  --path /baai-cwm-1/baai_cwm_ml/cwm/shaocong.xu/exp/tools/logs
python get_dir_size.py  --path /baai-cwm-1/baai_cwm_ml/cwm/shaocong.xu/



飞书机器人: https://open.feishu.cn/open-apis/bot/v2/hook/4ef7b048-cb79-4626-a98d-1ffa71d9d035



python -c 'import os; print(os.path.islink("/baai-cwm-1/baai_cwm_ml/cwm/tianqi.liu/anaconda3/pkgs/libasprintf-devel-0.22.5-he8f35ee_3/lib/libasprintf.so"))'
"""



import os
import os.path
import argparse
import glob
from os.path import join, split, exists,isfile, isdir,getsize
import requests
import json
from  tqdm import tqdm

import time 
import datetime




def get_data(cur_string):
    data = {
        "msg_type": "text",
        "content": {
            "text": cur_string
        }
    }
    return json.dumps(data,ensure_ascii=True).encode("utf-8")
    
def req(data):
	#webhook
    url = "https://open.feishu.cn/open-apis/bot/v2/hook/4ef7b048-cb79-4626-a98d-1ffa71d9d035"
    header = {
        "Content-type": "application/json",
        "charset":"utf-8"
    }
    requests.post(url,data=data,headers=header)



parser = argparse.ArgumentParser()
parser.add_argument('--path', type=str, help='the path to calculate the size')
args = parser.parse_args()



def get_size(path):
    """
    Get the size of all files in the given path, including subdirectories.

    Parameters
    ----------
    path : str
        The path to get the size of.

    Returns
    -------
    None
    """

    fileList = os.listdir(path)  # 获取path目录下所有文件
    for filename in fileList:
        pathTmp = os.path.join(path,filename)  # 获取path与filename组合后的路径
        if os.path.isdir(pathTmp):   # 判断是否为目录
            get_size(pathTmp)        # 是目录就继续递归查找
        elif os.path.isfile(pathTmp):  # 判断是否为文件
            filesize = getsize(pathTmp)  # 如果是文件，则获取相应文件的大小
            # print('目录中的子文件大小：%d字节' % filesize)
            list1.append(filesize)      # 将文件的大小添加到列表




def get_directory_size(directory):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(directory):

        for f in filenames:
            fp = os.path.join(dirpath, f)

            if not os.path.islink(fp):
                total_size += os.path.getsize(fp)

    return total_size


if __name__ == "__main__":

     
    
     
    
    print(time.asctime( time.localtime(time.time()) ))
    
    for p in tqdm(glob.glob(args.path+"/*")):
        print('calculating: %s'%(p))
        t0 = time.perf_counter()
        if isdir(p):
            # list1 = []
            # get_size(p)
            # size = sum(list1)
            size = get_directory_size(p)
            
            cur_str = '%s: %.3f GB' % ( p, size/1024/1024/1024)
            # print('目录中的文件总大小：%.2fB == %.2fKB == %.2f MB == %.2f GB' % (size, size/1024,size/1024/1024, size/1024/1024/1024))
        else:
            size = getsize(p)
            gb = size/1024/1024/1024
            if gb <  5:
                continue
            cur_str = '%s: %.3f GB' % ( p,gb )
        
        spend_time = time.perf_counter() - t0
        cur_str += '\t\t  spend time: %.3f m'%(spend_time/60)
        

    
        req(get_data(cur_str))
        print(cur_str)
